﻿using HtmlAgilityPack;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;

namespace HuabanAutoPost.Moetutu
{
    class Scanner
    {
        public static void Start(int StartPage, int EndPage)
        {

            if (StartPage < 1)
                StartPage = 1;

            for (int i = StartPage; i <= EndPage; i++)
            {

                AnalysisUrl("http://pic.moetutu.com/page/" + i);
            }
        }

        private static void AnalysisUrl(string Url)
        {
            String Shtml = Helper.WebHelper.GetHtml(Url);

            HtmlNodeCollection htmlNodeCollection = Helper.HtmlHelper.SelectNodes(Shtml, "//div[@class='post-thumb']");
            if (htmlNodeCollection != null)

                foreach (HtmlNode SingleNode in htmlNodeCollection)
                {


                    String Link = Helper.HtmlHelper.FirstValue(Helper.HtmlHelper.GetLinkVlaueList(SingleNode.InnerHtml, "//a[@class='thumb-holder-link']"));
                    Console.WriteLine("获取到链接" + Link);
                    String ImgLink = Helper.HtmlHelper.FirstValue(Helper.HtmlHelper.GetImgSrcVlaueList(SingleNode.InnerHtml, "//a[@class='thumb-holder-link']/img"));
                    Console.WriteLine("获取到图片" + ImgLink);
                    String Content = Regex.Replace(SingleNode.InnerText.Trim(), "\n", " ") + " - 萌图兔|http://pic.moetutu.com";
                    Console.WriteLine(Content);
                    if (Link != "" &&
                        ImgLink != ""
                     && Content != "") {
                        int Num = new Random().Next(5, 15);
                        Console.WriteLine();
                        //随机睡眠，模拟用户操作
                        Thread.Sleep(Num);
                        HuabanHelper.Publish.GetTicket(ImgLink, Content, Content, Link);
                        Console.WriteLine("已发布");
                    }
                }
        }
    }
}
